package com.yanqu.road.dao.impl.activity.ghost;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ghost.GhostData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GhostDataDaoImpl extends TempDataObjectDao<GhostData> {

    public static String getGhostDataSql = "select * from t_c_activity_ghost_data where activityId=?";

    public GhostData getGhostData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getGhostDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateGhostDataSql = "update t_c_activity_ghost_data set lastResetTime=?,maxBatchNo=?,abilityMap=?,avgAbility=?,avgOpenDay=?,costAbility=?,batchBossTotalHp=?,unionChest=? where activityId=?";

    @Override
    protected boolean updateData(GhostData ghostData) {
        ghostData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, ghostData.getLastResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostData.getMaxBatchNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(ghostData.getAbilityMap())));
        para.put(i++, new DbParameter(Types.BIGINT, ghostData.getAvgAbility()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostData.getAvgOpenDay()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostData.getCostAbility()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostData.getBatchBossTotalHp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(ghostData.getUnionChest())));
        para.put(i++, new DbParameter(Types.INTEGER, ghostData.getActivityId()));
        if (getDb().executeNoneQuery(updateGhostDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGhostDataSql = "insert into t_c_activity_ghost_data(activityId,lastResetTime,maxBatchNo,abilityMap,avgAbility,avgOpenDay,costAbility,batchBossTotalHp,unionChest) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(GhostData ghostData) {
        ghostData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, ghostData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostData.getLastResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostData.getMaxBatchNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(ghostData.getAbilityMap())));
        para.put(i++, new DbParameter(Types.BIGINT, ghostData.getAvgAbility()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostData.getAvgOpenDay()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostData.getCostAbility()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostData.getBatchBossTotalHp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(ghostData.getUnionChest())));

        if (getDb().executeNoneQuery(insertGhostDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected GhostData parseData(ResultSet rs) throws SQLException {
        GhostData ghostData = new GhostData();
        ghostData.setActivityId(rs.getInt("activityId"));
        ghostData.setLastResetTime(rs.getLong("lastResetTime"));
        ghostData.setMaxBatchNo(rs.getInt("maxBatchNo"));
        String abilityMap = rs.getString("abilityMap");
        ghostData.setAbilityMap(StringUtils.isNullOrEmpty(abilityMap) ? new ConcurrentHashMap<>() : JSONObject.parseObject(abilityMap, new TypeReference<Map<Long, List<Long>>>() {
        }));
        ghostData.setAvgAbility(rs.getLong("avgAbility"));
        ghostData.setAvgOpenDay(rs.getInt("avgOpenDay"));
        ghostData.setCostAbility(rs.getLong("costAbility"));
        ghostData.setBatchBossTotalHp(rs.getLong("batchBossTotalHp"));

        String unionChest = rs.getString("unionChest");
        ghostData.setUnionChest(StringUtils.isNullOrEmpty(unionChest) ? new ConcurrentHashMap<>() : JSONObject.parseObject(unionChest, new TypeReference<Map<String, Map<Integer, Integer>>>() {
        }));

        return ghostData;
    }
}
